package com.cskt.itripauth.controller;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskt.common.constants.ErrorCodeEnum;
import com.cskt.common.entity.ItripUser;
import com.cskt.common.model.ItripUserModel;
import com.cskt.itripauth.service.impl.AuthTripUserServiceImpl;
import com.cskt.service.impl.ItripUserServiceImpl;
import com.cskt.common.vo.ReturnResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
public class UserController {
    @Resource
    private ItripUserServiceImpl itripUserService;
    @Resource
    private AuthTripUserServiceImpl authTripUserService;

    @GetMapping("/checkUser")
    public ReturnResult checkUser(String userCode){
        //判断userCode是否未空
        if (userCode == null||userCode==""||userCode.isEmpty()) {
            return ReturnResult.error(ErrorCodeEnum.USERNAME_VERIFICATION_NULL);
        }
        QueryWrapper<ItripUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_code", userCode);
        ItripUser itripUser = itripUserService.getOne(queryWrapper);
        //账号有了，并且还是激活状态的，那么就不可以再进行注册
        //如果账号有，但是没有激活，也就是邮箱注册的时候，没有进行登录网址激活，那么还可以重新进行注册
        if (itripUser != null&&itripUser.getActivated()==1) {
            return ReturnResult.error(ErrorCodeEnum.USERNAME_ALREADY_EXISTS);
        }
        return ReturnResult.ok();
    }

    @GetMapping("/sendSMS")
    public ReturnResult sendSMS(String phone) {
        //判断手机号是否为空
        if (phone.isEmpty() || phone == null || phone == "") {
            return ReturnResult.error(ErrorCodeEnum.USERNAME_VERIFICATION_NULL);
        } else {
            authTripUserService.sendSMS(phone);
            return ReturnResult.ok();
        }
    }

    @PostMapping("/registerSMS")
    public ReturnResult register(@RequestBody ItripUserModel itripUserModel){
        if (itripUserModel.getCode()==null||itripUserModel.getCode().isEmpty()) {
            return ReturnResult.error(ErrorCodeEnum.SMS_VERIFICATION_CODE_INPUT_NULL);
        }
        String json= JSON.toJSONStringWithDateFormat(itripUserModel,"yyyy-MM-dd HH:mm:ss.SSS");
        ItripUser itripUser = JSON.parseObject(json, ItripUser.class);
        authTripUserService.registerSMS(itripUser,itripUserModel.getCode());
        return ReturnResult.ok();
    }

    //邮箱注册的时候，不包含验证码，而是发送邮件中提供了一个激活地址
    @PostMapping("/registerEmail")
    public ReturnResult registerEmail(@RequestBody ItripUser itripUser){
        if (StringUtils.isEmpty(itripUser.getUserCode())) {
            return ReturnResult.error(ErrorCodeEnum.USERNAME_VERIFICATION_NULL);
        }
        authTripUserService.registerEmail(itripUser);
        return ReturnResult.ok();
    }

    //激活
    @GetMapping("/activation/{key}")
    public ReturnResult activation(@PathVariable("key") String key){
        authTripUserService.activation(key);
        return ReturnResult.ok();
    }
}
